
package me.module.toolMain.mainElement
{
	import adobe.utils.MMExecute;
	
	import com.zero.util.InstanceUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import me.common.GlobalCore;
	import me.vo.ActionVO;
	
	
	/**
	 * @author 缺硒
	 */
	public class ActionWindow extends Sprite
	{
		private var _animator:BitmapPlayer;
		private var _line:int;
		private var _action:ActionVO;
		private var _isPlaying:Boolean = true;
		private var _backgroundColor:uint = 0xFFFFFF;
		private var _axisShap:Shape;

		
		// 构造函数
		public function ActionWindow()
		{
			_animator = InstanceUtil.create(new BitmapPlayer(), this);
			_axisShap = InstanceUtil.create(new Shape(), this);
			
			this.graphics.lineStyle(1, 0xcccccc);
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRect(0,0,200,200);
			this.graphics.endFill();
			

			
			scrollRect = new Rectangle(0,0,200,200);
		}


		public function get action():ActionVO
		{
			return _action;
		}

		public function set action(value:ActionVO):void
		{
			_action = value;
		}

		/**
		 * 增加精灵资源
		 * @param bmd
		 */		
		public function addAnimator():void
		{
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			_animator.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_animator.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			_action = GlobalCore.actions[0];
			_animator.play(_action,0);
			play();
			_animator.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_animator.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		/**
		 * 重绘精灵预览窗口
		 * @param w
		 * @param h
		 */		
		public function drawWin(w:int, h:int):void
		{
			_axisShap.graphics.clear();
			_axisShap.graphics.lineStyle(1, 0xFF0000, 0.7);
			_axisShap.graphics.moveTo(w/2,0);
			_axisShap.graphics.lineTo(w/2,h);
			_axisShap.graphics.moveTo(0,h/2);
			_axisShap.graphics.lineTo(w,h/2);
			_axisShap.graphics.endFill();
			
			var winOffX:int = (GlobalCore.spritePreW - w) / 2;
			var winOffY:int = (GlobalCore.spritePreH - h) / 2;
			
			this.graphics.clear();
			this.graphics.lineStyle(1, 0xcccccc);
			this.graphics.beginFill(_backgroundColor);
			this.graphics.drawRect(winOffX,winOffY,w,h);
			this.graphics.endFill();
			
			_axisShap.x = winOffX;
			_axisShap.y = winOffY;

			scrollRect = new Rectangle(winOffX,winOffY,w,h);
		}
		
		
		private function onEnterFrame(evt:Event):void
		{
			if(_isPlaying)
			_animator.play(_action,line);
		}
		
		private function onMouseDown(evt:MouseEvent):void
		{
			_animator.startDrag();
		}
		
		private function onMouseUp(evt:MouseEvent):void
		{
			_animator.stopDrag();
		}
		

		
		public function play():void
		{
			_isPlaying = true;
		}
		
		public function stop():void
		{
			_isPlaying = false;
		}
		
		/**
		 * 当前帧数
		 * @return  
		 */		
		public function get currentFrame():int
		{
			return _animator.currentFrame;
		}
		
		/**
		 * 动作行
		 */	
		public function get line():int
		{
			return _line;
		}
		
		public function set line(value:int):void
		{
			_line = value;
		}
		
		/**
		 * 返回精灵的x偏移量
		 * @return  
		 */		
		public function get offsetX():Number
		{
			return _animator.x;
		}
		
		public function set offsetX(value:Number):void
		{
			_animator.x = value;
		}
		
		/**
		 * 返回精灵的y偏移量
		 * @return 
		 */		
		public function get offsetY():Number
		{
			return _animator.y;
		}
		
		public function set offsetY(value:Number):void
		{
			_animator.y = value;
		}

		public function get backgroundColor():uint
		{
			return _backgroundColor;
		}

		public function set backgroundColor(value:uint):void
		{
			_backgroundColor = value;
		}


		//
	}
}